package com.xzr.hospitalregister.util;

import android.os.Handler;
import android.os.Message;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/5/23 0023.
 */

public class HttpRequest {
    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    public final static int CONNECT_TIMEOUT = 10;
    public final static int READ_TIMEOUT = 50;
    public final static int WRITE_TIMEOUT = 30;

    private OkHttpClient client = new OkHttpClient.Builder()
//            .connectTimeout(10, TimeUnit.SECONDS)
//            .readTimeout(20, TimeUnit.SECONDS)
            .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)//设置读取超时时间
            .writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS)//设置写的超时时间
            .connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)//设置连接超时时间
            .build();

    private static HttpRequest httpRequest = new HttpRequest();

    private HttpRequest() {
    }

    public static HttpRequest getInstance() {
        return httpRequest;
    }

    public void getRequest(String url, final Handler handler) {
        Request request = new Request.Builder()
                .url(url)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Message message = handler.obtainMessage();
                message.obj = e.toString();
                message.arg1 = -1;
                handler.sendMessage(message);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    Message message = handler.obtainMessage();
                    message.obj = response.body().string();
                    message.arg1 = 1;
                    handler.sendMessage(message);
                }
            }
        });
    }

    public void postRequest(String url, String json, final Handler handler) {
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Message message = handler.obtainMessage();
                message.obj = e.toString();
                message.arg1 = -1;
                handler.sendMessage(message);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    Message message = handler.obtainMessage();
                    message.obj = response.body().string();
                    message.arg1 = 1;
                    handler.sendMessage(message);
                }
            }
        });
    }


    public interface DealResultInBackgroud {

        void dealResult(Integer resultState, String result);
    }

    public void postRequestDealResultInBackgroud(String url, String json, final DealResultInBackgroud dealResultInBackgroud) {
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                dealResultInBackgroud.dealResult(-1, e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    dealResultInBackgroud.dealResult(1, response.body().string());
                }
            }
        });
    }

    public interface DealResultInBackgroudAndChangeUI {
        void dealResultAndChangeUI(Integer resultState, String result, Handler handler);
    }

    public void getRequestDealResultInBackgroudAndChangeUI(String url, final DealResultInBackgroudAndChangeUI dealResultInBackgroudAndChangeUI, final Handler handler) {
        Request request = new Request.Builder()
                .url(url)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                dealResultInBackgroudAndChangeUI.dealResultAndChangeUI(-1, e.toString(), handler);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    dealResultInBackgroudAndChangeUI.dealResultAndChangeUI(1, response.body().string(), handler);
                }
            }
        });
    }
    //////////////
    public void getRequestDealResultInBackgroud(String url, final DealResultInBackgroud dealResultInBackgroud) {
        Request request = new Request.Builder()
                .url(url)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                dealResultInBackgroud.dealResult(-1, e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    dealResultInBackgroud.dealResult(1, response.body().string());
                }
            }
        });


    }
    ///////////////

}
